package com.southminority.ethnic.service.student.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southminority.ethnic.controller.student.vo.ArticlePageVo;
import com.southminority.ethnic.pojo.StudentArticle;
import com.southminority.ethnic.service.student.StudentArticleService;
import com.southminority.ethnic.mapper.student.StudentArticleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author SouthMinority
 * @description 针对表【student_article(学生作品表)】的数据库操作Service实现
 * @createDate 2025-11-17
 */
@Service
public class StudentArticleServiceImpl extends ServiceImpl<StudentArticleMapper, StudentArticle>
    implements StudentArticleService {

    @Override
    public Boolean incrementViews(Long id) {
        StudentArticle article = this.getById(id);
        if (article != null) {
            article.setViews(article.getViews() == null ? 1 : article.getViews() + 1);
            return this.updateById(article);
        }
        return false;
    }

    @Override
    public Page<ArticlePageVo> getArticlePage(Integer pageNum, Integer pageSize, String keyword, Integer status) {
        Page<StudentArticle> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<StudentArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentArticle::getIsDeleted, 0);
        
        // 状态筛选：如果status不为null，则按指定状态筛选；否则查询全部
        if (status != null) {
            queryWrapper.eq(StudentArticle::getStatus, status);
        }
        
        if (keyword != null && !keyword.trim().isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                .like(StudentArticle::getTitle, keyword)
                .or()
                .like(StudentArticle::getAuthor, keyword)
                .or()
                .like(StudentArticle::getKeywords, keyword)
            );
        }
        
        queryWrapper.orderByDesc(StudentArticle::getPublishDate)
                   .orderByDesc(StudentArticle::getCreateTime);
        //查询包含ArticlePageVo的字段
        queryWrapper.select(
                StudentArticle::getId,
                StudentArticle::getTitle,
                StudentArticle::getAuthor,
                StudentArticle::getJournal,
                StudentArticle::getPublishDate,
                StudentArticle::getAbstractText,
                StudentArticle::getKeywords,
                StudentArticle::getViews,
                StudentArticle::getStatus,
                StudentArticle::getCreateTime,
                StudentArticle::getUpdateTime
        );
        Page<StudentArticle> articlePage = this.page(page, queryWrapper);
        
        // 转换为VO，不包含完整内容
        Page<ArticlePageVo> voPage = new Page<>(articlePage.getCurrent(), articlePage.getSize(), articlePage.getTotal());
        voPage.setRecords(articlePage.getRecords().stream().map(article -> {
            ArticlePageVo vo = new ArticlePageVo();
            BeanUtils.copyProperties(article, vo);
            // 摘要截取前200字
            if (vo.getAbstractText() != null && vo.getAbstractText().length() > 200) {
                vo.setAbstractText(vo.getAbstractText().substring(0, 200) + "...");
            }
            return vo;
        }).toList());
        
        return voPage;
    }
}
